/*
* 文 件 名:  RoleDAOImpl.java
* 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2012,  All rights reserved
* 描    述:  角色操作接口实现
* 修 改 人:  aganzhoul
* 修改时间:  2012-9-11
* 跟踪单号:  <跟踪单号>
* 修改单号:  <修改单号>
* 修改内容:  <修改内容>
*/

package com.tydic.simple.dao.user.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.tydic.framework.dao.AbstractIbatisDAO;
import com.tydic.framework.dataobject.ResultDTO;
import com.tydic.framework.exception.DAOException;
import com.tydic.simple.dao.user.IRoleDAO;
import com.tydic.simple.dto.user.RoleDTO;
import com.tydic.simple.model.user.Role;

/**
* 角色操作接口实现
* 
*  @author  aganzhoul
* @version  [1.0, 2012-9-11]
* @since  [权限管理/1.0]
*/

public class RoleDAOImpl extends AbstractIbatisDAO implements IRoleDAO {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(RoleDAOImpl.class);
    
    /** {@inheritDoc} */
    @Override
    public Long saveRole(Role role)
        throws DAOException {
        /*if( LOGGER.isDebugEnabled() ){
            LOGGER.debug("RoleDAOImpl.saveRole begin...");
        }*/
        try {
            sqlMapClient.insert("user.INSERT_ROLE", role);
        }
        catch (SQLException e) {
            LOGGER.error("saveRole exception",e);
            throw new DAOException("saveRole exception", e);
        }
        Long result = role.getRoleId();
        /*if( LOGGER.isDebugEnabled() ){
            LOGGER.debug("RoleDAOImpl.saveRole end...");
        }*/
        return result;
    }
    
    /** {@inheritDoc} */
    @Override
    public int deleteRoleByCode(String roleCode)
        throws DAOException {
        try {
            return sqlMapClient.delete("user.DELETE_ROLE_BY_CODE", roleCode);
        }
        catch (SQLException e) {
            LOGGER.error("deleteRoleByCode exception",e);
            throw new DAOException("deleteRoleByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @Override
    public Role findRoleByCode(String roleCode)
        throws DAOException {
        try {
            return (Role)sqlMapClient.queryForObject("user.FIND_ROLE_BY_CODE", roleCode);
        }
        catch (SQLException e) {
            LOGGER.error("findRoleByCode exception",e);
            throw new DAOException("findRoleByCode exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public ResultDTO<Role> findRoleForPage(RoleDTO roleDTO)
        throws DAOException {
        ResultDTO<Role> result = new ResultDTO<Role>();
        try {
            List<Role> tempList = sqlMapClient.queryForList("user.FIND_ROLE_PAGE", roleDTO);
            result.setResult(tempList != null ? tempList : new ArrayList<Role>());
            BeanUtils.copyProperties(roleDTO.getPagination(), result.getPagination());
            result.getPagination().setTotal((Integer)sqlMapClient.queryForObject("user.FIND_ROLE_PAGE_COUNT", roleDTO));
        }
        catch (SQLException e) {
            LOGGER.error("findRoleForPage exception",e);
            throw new DAOException("findRoleForPage exception", e);
        }
        return result;
    }
    
    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public List<Role> findRoleForList(RoleDTO roleDTO)
        throws DAOException {
        try {
            List<Role> tempList = sqlMapClient.queryForList("user.FIND_ROLE", roleDTO);
            return tempList != null ? tempList : new ArrayList<Role>();
        }
        catch (SQLException e) {
            LOGGER.error("findRoleForList exception",e);
            throw new DAOException("findRoleForList exception", e);
        }
    }
    
    /** {@inheritDoc} */
    @Override
    public Role findRoleForSingle(RoleDTO roleDTO)
        throws DAOException {
        List<Role> roleList = findRoleForList(roleDTO);
        return roleList.size() > 0 ? roleList.get(0) : null;
    }
    
    /** {@inheritDoc} */
    @Override
    public int updateRole(Role role)
        throws DAOException {
        try {
            return sqlMapClient.update("user.UPDATE_ROLE_BY_CODE", role);
        }
        catch (SQLException e) {
            LOGGER.error("updateRole exception",e);
            throw new DAOException("updateRole exception", e);
        }
    }
    
}
